package org.leo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Connector {
    Connection connection;

    public Connector() {
        // This is a constructor for the Connector class.
        // You can initialize any resources or configurations here if needed.
        System.out.println("Connector initialized.");
        
    }
    public static void main(String[] args) {
        Connector connector = new Connector();
        if (connector.getConnection("192.168.122.9", 3306, "leo", "leo130")) {
            System.out.println("Connection successful!");
            ResultSet rs = connector.sqlSend("show databases;");
            try {
                while (rs.next()) {
                    System.out.println("Data: " + rs.getString(1)); // Adjust based on your table structure
                }
            } catch (SQLException e) {
                System.err.println("Error reading result set: " + e.getMessage());
            } finally {
                closeConnection(connector.connection);
            }
        } else {
            System.out.println("Connection failed.");
        }
        
   
    }
    public  boolean getConnection(String ip,Integer port, String user, String password) {
        // This method should return a database connection.
        // For now, it returns null as a placeholder.
        final String Port = port != null ? port.toString() : "3306";
        final String Ip = ip != null ? ip : "localhost";
        
        final String Password = password != null ? password : "";
        final String User = user != null ? user : "root";
        String url = "jdbc:mysql://" + Ip + ":" + Port ;
        System.out.println("Connecting to database at " + url + " with user " + user);
        // Here you would normally use DriverManager.getConnection(url, user, password);
        try {
            this.connection = DriverManager.getConnection(url, User, Password);
            System.out.println("Connection established successfully.");
            return true;
        } catch (SQLException e) {
            System.err.println("Connection failed: " + e.getMessage());
        }
        
        return false;
    }
    public static void closeConnection(Connection connection) {
        // This method should close the database connection.
        // For now, it does nothing as a placeholder.
        if (connection != null) {
            try {
                connection.close();
                System.out.println("Connection closed successfully.");
            } catch (SQLException e) {
                System.err.println("Failed to close connection: " + e.getMessage());
            }
        } else {
            System.out.println("No connection to close.");
        }

    }
    public ResultSet sqlSend(String sql) {
        // This method should execute a SQL command.
        // For now, it does nothing as a placeholder.
        System.out.println("Executing SQL command: " + sql);
        // Here you would normally use a Statement or PreparedStatement to execute the SQL command.
        Connection connection = null;
        try {
            connection = this.connection;
            Statement stm =  connection.createStatement();
            ResultSet res = stm.executeQuery(sql);
            return res;
        } catch (SQLException e) {
            System.err.println("SQL execution failed: " + e.getMessage());
            return null;
        } finally {
            closeConnection(connection);  
            
        }
    }
}
